﻿using System;
using System.Configuration;
using System.Reflection;

namespace _05_AbstractFactoryModel_Reflection
{
    class Program
    {
        static void Main(string[] args)
        {
            User user = new User();
            Department dept = new Department();

            IUser iUser = DataAccess.CreateUser();
            iUser.Insert(user);
            iUser.GetUser(1);

            IDepartment iDepartment = DataAccess.CreateDepartment();
            iDepartment.Insert(dept);
            iDepartment.GetDepartment(1);
            Console.Read();
        }
    }
    class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    interface IUser
    {
        void Insert(User user);
        User GetUser(int id);
    }
    class SqlserverUser : IUser
    {
        public void Insert(User user)
        {
            Console.WriteLine("在SQL Server中给User表增加一条记录");
        }
        public User GetUser(int id)
        {
            Console.WriteLine("在SQL Server中根据ID得到User表一条记录");
            return null;
        }
    }
    class AccessUser : IUser
    {
        public void Insert(User user)
        {
            Console.WriteLine("在Access中给User表增加一条记录");
        }
        public User GetUser(int id)
        {
            Console.WriteLine("在Access中根据ID得到User表一条记录");
            return null;
        }
    }
    class Department
    {
        public int ID { get; set; }

        public string DeptName { get; set; }
    }
    interface IDepartment
    {
        void Insert(Department user);
        Department GetDepartment(int id);
    }
    class SqlserverDepartment : IDepartment
    {
        public void Insert(Department department)
        {
            Console.WriteLine("在Sqlserver中给Department表增加一条记录");
        }

        public Department GetDepartment(int id)
        {
            Console.WriteLine("在Sqlserver中根据ID得到Department表一条记录");
            return null;
        }
    }

    class AccessDepartment : IDepartment
    {
        public void Insert(Department department)
        {
            Console.WriteLine("在Access中给Department表增加一条记录");
        }

        public Department GetDepartment(int id)
        {
            Console.WriteLine("在Access中根据ID得到Department表一条记录");
            return null;
        }
    }
    class DataAccess
    {
        private static readonly string AssemblyName = "05 AbstractFactoryModel_Reflection";
        //private static readonly string db = "Sqlserver"; //"Access"
        private static readonly string db = ConfigurationManager.AppSettings["DB"];


        public static IUser CreateUser()
        {
            string className = "_05_AbstractFactoryModel_Reflection" + "." + db + "User";
            return (IUser)Assembly.Load(AssemblyName).CreateInstance(className);
        }
        public static IDepartment CreateDepartment()
        {
            string className = "_05_AbstractFactoryModel_Reflection" + "." + db + "Department";
            return (IDepartment)Assembly.Load(AssemblyName).CreateInstance(className);
        }
    }
}